using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;
using WinSAG.Properties;

namespace WinSAG
{
    [DefaultPropertyAttribute("ToolName"), Serializable()]
    public class SAGPreferences : ICloneable, IGridProperties
    {        
        // SAG properties
        private string toolName;
        private string toolPath;
        private string toolResultStyleSheet;
        private int toolTimeOut;
        
        internal SAGPreferences()
        {            
            toolName = Settings.Default.ToolName;
            toolPath = Settings.Default.ToolPath;
            toolResultStyleSheet = Settings.Default.ToolStyleSheet;
            toolTimeOut = Settings.Default.ToolTimeOut;

            if (toolTimeOut == 0)
                toolTimeOut = WSU_Literals.SAT_WAITTIME;    // Resort to default time

            try
            {
                if (File.Exists(this.getToolToExecute()) == false)
                {
                    toolName = WSU_Literals.SAT_NAME;
                    toolPath = Application.UserAppDataPath;
                }

            }
            catch (System.Exception) { }
        }

        
        internal string getToolToExecute()
        {
            return  this.toolPath + 
                        (
                            this.toolPath.EndsWith(@"\") == true ? this.toolName : 
                            (@"\" + this.toolName)
                        );            
        }

        //******************************************************************************************
        // Property grid variables
        //
        [CategoryAttribute("Preferences"), ReadOnlyAttribute(true),
        DefaultValueAttribute(""), 
        DescriptionAttribute("Tool name. Select the path below to load this EXE.")]
        public string ToolName
        {
            get { return toolName; }
            set { toolName = value; }
        }

        [CategoryAttribute("Preferences"), EditorAttribute(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
        DescriptionAttribute("Tool Path. Select the EXE file you want to use for finding duplicate code.")]
        public string ToolPath
        {
            get { return toolPath;  }
            set 
            {
                toolName = Path.GetFileName(value);
                toolPath = Path.GetDirectoryName(value);
            }
        }

        [CategoryAttribute("Preferences"), EditorAttribute(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
        DescriptionAttribute("Stylesheet to apply to the tool results. Ex., simian.xsl")]
        public string StyleSheet
        {
            get { return toolResultStyleSheet; }
            set { toolResultStyleSheet = value; }
        }

        [CategoryAttribute("Preferences"), 
        DescriptionAttribute("Maximum time to wait for the tool execution to end. This is specified in milliseconds")]
        public int TimeOut
        {
            get { return toolTimeOut; }
            set { toolTimeOut = value; }
        }
        //******************************************************************************************

        #region ICloneable Members

        public object Clone()
        {
            // Cloning is shallow - TODO: Check if this is enuf or more needed. 
            return (this.MemberwiseClone() as SAGPreferences);            
        }

        #endregion

        #region IGridProperties Members

        private bool dirtyFlag = false;

        public void setDirty(bool v)
        {
            dirtyFlag = v;
        }

        public bool IsDirty()
        {
            return dirtyFlag;
        }

        public string getObjectName()
        {
            return @"Preferences";
        }

        #endregion
    }
}
